<?php
/**
 * @author zxg
 * 读取excel 只处理第一个sheet 
 */
class ZScoreImport{
    private $filePath;
    private static $shift0 = null;
    CONST BATCHNUM = 500;
    CONST UPLOAD_SCORE = 1;
    CONST UPLOAD_STUDENT = 2;
    
    private $titleLineNum = 0;
    
            
    /**
     * construct
     * @param type $fileName excel 存储全路径
     * @param type $titleLineNum excel 标题行位置
     */
    public function __construct($fileName,$titleLineNum=0) {
        $this->filePath = $fileName;
        $this->titleLineNum = $titleLineNum;
    }
    /**
     * 分数excel存储位置
     * @return type 
     */
    public static function getExcelSavePath($buss=self::UPLOAD_SCORE){
        switch ($buss){
            case self::UPLOAD_SCORE:
                return UPLOAD_PATH . '/scores/';
                break;
            case self::UPLOAD_STUDENT:
                return UPLOAD_PATH . '/info/';
                break;
            default:
                return false;
                break;
        }
    }
    /**
     * 加载PHPExcel类文件  
     */
    private  function includePHPExcel(){
        $includePath = Yii::getPathOfAlias("ext");
        spl_autoload_unregister(array('YiiBase','autoload'));
        include_once $includePath.'/PHPExcel.php';
        include_once $includePath.'/PHPExcel/IOFactory.php';
        spl_autoload_register(array('YiiBase','autoload'));
    }
    /**
     * excel 第一个sheet
     * @return boolean 
     */
    public function getShift0(){$begin = time();
        if(!empty(self::$shift0))
            return self::$shift0;
        if(!file_exists($this->filePath) && is_file($this->filePath))
            return false;
        $type = self::getExcelType($this->filePath);
        $this->includePHPExcel();
        $xlsReader = PHPExcel_IOFactory::createReader($type);
        $xlsReader->setReadDataOnly(true);
        $xlsReader->setLoadSheetsOnly(true);
        $Sheets = $xlsReader->load($this->filePath);
        echo time() - $begin;exit;
        return self::$shift0 = $Sheets->getSheet(0);
    }
    /**
     * 获取excel表头
     * @return type 
     */
    public function fetchTitle(){
        return $this->fetchRow($this->titleLineNum);
    }
    /**
     * 从excel获取一行数据
     * @param type $rowNum 
     */
    public function fetchRow($rowNum){
        $currentSheet = $this->getShift0();
        $rowMax = $currentSheet->getHighestRow();
        if($rowNum>$rowMax){
            return false;
        }
        $colMax = $currentSheet->getHighestColumn();
        $colMax = ZCommon::excelColNameToNum($colMax);
        $cell_values = null;
        for($j = 1; $j <= $colMax; $j++) { 
            $address = ZCommon::numToExcelColName($j) . $rowNum; // 单元格坐标 
            $cell_values[] = $currentSheet->getCell($address)->getFormattedValue(); ;
        }
        return $cell_values;
    }
    /**
     * 获取excel总行数 
     */
    public function fetchCount(){
        return $currentSheet = $this->getShift0()->getHighestRow();
    }
    /**
     * 分批获取excel数据
     */
    public  function fetchBatchData($batch){
        $pageRowBegin = ($batch-1)*self::BATCHNUM + $this->titleLineNum + 1;
        $pageRowEnd = $pageRowBegin + self::BATCHNUM - 1; 
        
        // sheet 未读取到末尾
        $rs['status'] = 2; 
        $rs['data'] = array();
        
        // 当前sheet读取完成
        $currentSheet = $this->getShift0(); // 当前页
        $rowMax = $currentSheet->getHighestRow();
        $colMax = $currentSheet->getHighestDataColumn();
        $colMax = ZCommon::excelColNameToNum($colMax);
        if($rowMax < $pageRowEnd){
            $rs['status'] = 1;
            $pageRowEnd = $rowMax;
        }
        // 获取数据 
        for($i = $pageRowBegin; $i <= $pageRowEnd; $i++){
            $cell_values = array();
            for($j = 1; $j <= $colMax; $j++){ 
                $address = ZCommon::numToExcelColName($j) . $i; // 单元格坐标 
                $cell_values[] = trim($currentSheet->getCell($address)->getFormattedValue()); 
            } 
            $rs['data'][] = $cell_values;
        } 
        return $rs;
    }
    
    /**
     * 获取excel类型
     */
    public static function getExcelType($fileName){
        $extend = self::getFileExtName($fileName);
        if ($extend == 'xlsx') {
            $type = 'Excel2007';
        }elseif ($extend=='xls') {
            $type = 'Excel5';
        }else {
            $type = false;
        }
        return $type;
    }
    public static function getFileExtName($fileName){
        $extend = pathinfo($fileName);
        return strtolower($extend["extension"]);
    }
}
?>
